package com.ustcinfo.study.hbase.r1.cuiyang;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.hadoop.hbase.client.Put;

public class MySqlSubmit {

    // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/test";

    // 数据库的用户名与密码，需要根据自己的设置
    static final String UserName = "root";
    static final String PassWord = "root";

    public static void main(String[] args){
        Connection conn=null;
        Statement stmt=null;

        // 注册 JDBC 驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection(DB_URL,UserName,PassWord);

            stmt = conn.createStatement();

            String sql = "SELECT * FROM transfer_task_to";

            ResultSet rs = stmt.executeQuery(sql);

//

            // 展开结果集数据库
            while(rs.next()){


                System.out.println(rs.getBytes("task_id"));
                System.out.println(rs.getBytes("create_time"));
                System.out.println(rs.getBytes("out_filter_number"));
                System.out.println(rs.getBytes("retry_flag"));
                System.out.println(rs.getBytes("failure_time"));
                System.out.println(rs.getBytes("end_time"));

                //插入操作  HBaseOperationDao
//字段：id,task_id,create_time,start_time,out_filter_number,from_run_param 写到列簇F1中,列名不变
//字段：current_retry_times,retry_flag,parent_task_id,end_time,kill_time,failure_time 写到列簇F2中,列名不变
                Put put = new Put(rs.getBytes("id"));  //row key
                put.addColumn("F1".getBytes(), "task_id".getBytes(), rs.getBytes("task_id"));  //簇 列 值
                put.addColumn("F1".getBytes(), "create_time".getBytes(), rs.getBytes("create_time"));
                put.addColumn("F1".getBytes(), "start_time".getBytes(), rs.getBytes("start_time"));
                put.addColumn("F1".getBytes(), "out_filter_number".getBytes(), rs.getBytes("out_filter_number"));
                put.addColumn("F1".getBytes(), "from_run_param".getBytes(), rs.getBytes("from_run_param"));

                put.addColumn("F2".getBytes(), "current_retry_times".getBytes(), rs.getBytes("current_retry_times"));
                put.addColumn("F2".getBytes(), "retry_flag".getBytes(), rs.getBytes("retry_flag"));
                put.addColumn("F2".getBytes(), "parent_task_id".getBytes(), rs.getBytes("parent_task_id"));
                put.addColumn("F2".getBytes(), "end_time".getBytes(), rs.getBytes("end_time"));
                put.addColumn("F2".getBytes(), "kill_time".getBytes(), rs.getBytes("kill_time"));
                put.addColumn("F2".getBytes(), "failure_time".getBytes(), rs.getBytes("failure_time"));

                HBaseOperationDao.put("BD_TEST", put);  //表名 ， put的值

            }



            // 完成后关闭
            rs.close();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (SQLException e) {

            e.printStackTrace();
        } finally{
            // 关闭资源
            try{
                if(stmt!=null)
                    stmt.close();
            }catch(SQLException se2){
            }
            try{
                if(conn!=null)
                    conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }

    }
}

